find_package(TINYXML2 CONFIG QUIET)
if(TINYXML2_FOUND)
    message(STATUS "Found tinyxml2 via config file: ${TINYXML2_DIR}")
    if(NOT TINYXML2_LIBRARY)
        if(TARGET tinyxml2)
            set(TINYXML2_LIBRARY tinyxml2)
        elseif(TARGET tinyxml2::tinyxml2)
            set(TINYXML2_LIBRARY tinyxml2::tinyxml2)
        endif()
    endif()
else()
    find_path(TINYXML2_INCLUDE_DIR NAMES tinyxml2.h)
    find_library(TINYXML2_LIBRARY tinyxml2)
    include(FindPackageHandleStandardArgs)
    find_package_handle_standard_args(TINYXML2 DEFAULT_MSG TINYXML2_LIBRARY TINYXML2_INCLUDE_DIR)
    mark_as_advanced(TINYXML2_INCLUDE_DIR TINYXML2_LIBRARY)
endif()

if (TINYXML2_FOUND)
    if(NOT TARGET tinyxml2::tinyxml2)
        add_library(tinyxml2::tinyxml2 INTERFACE IMPORTED)
    endif()
    set_target_properties(tinyxml2::tinyxml2 PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${TINYXML2_INCLUDE_DIR}"
        INTERFACE_LINK_LIBRARIES "${TINYXML2_LIBRARY}")
    if(NOT TINYXML2_LIBRARY)
        unset(TINYXML2_LIBRARY CACHE)
    endif()
    if(NOT TINYXML2_INCLUDE_DIR)
        unset(TINYXML2_INCLUDE_DIR CACHE)
    endif()
endif()
